<?php


declare(strict_types=1);

/**
 * WxvisitController.php
 * 微信小程序访问统计
 * User：Abell
 * Date：2020/4/17
 * Time：14:00
 */

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Utils\ApiUtils;
use App\Model\Wxvisit as WxvisitModel;
use App\Model\Wxuser as WxuserModel;

/**
 * @Controller()
 */
class WxvisitController extends AbstractController
{
    /**
     * 累计访问人数
     * Abell 2020/4/26
     * @RequestMapping(path="/abell/wxvisit/deltime", methods="get")
     */
    public function deltime(RequestInterface $request){
        $today_zero = strtotime(date('Y-m-d', time()));
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近19天的数据
            $start_time  = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 19;
            $end_time = time();
        }
        var_dump('开始时间类型');
        var_dump(gettype($start_time));
        var_dump(gettype($end_time));
        var_dump('开始时间减去标准时间');
        $stand_time = strtotime(date('Y-m-d', time()));
        var_dump($start_time - $stand_time);
        $result = parallel([
            function() use ($stand_time,$start_time){
            var_dump('协程一');
                var_dump($stand_time);
                var_dump($start_time);
                return 'hello1';
            },
            function() use ($stand_time,$start_time){
                var_dump('协程二');
                var_dump($stand_time);
                var_dump($start_time);
                return 'hello2';
            }
        ]);
        return $result;
    }

    /**
     * 累计访问人数
     * Abell 2020/4/26
     * @RequestMapping(path="/abell/wxvisit/visithistory", methods="get")
     */
    public function visithistory(RequestInterface $request){
        $weeks_data = [];
        $today_data = 0;
        $today_zero = strtotime(date('Y-m-d', time()));
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近19天的数据
            $start_time = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 19;
            $end_time = time();
        }
        // 搜索结束时间是当天的话就生成当天的统计
        if($end_time > $today_zero){
            $today_data = WxvisitModel::query()->where(['p_id'=>0])->where('add_time', '>', $today_zero)->count();
        }
        // 判断搜索时间段的数据是否存在,不存在就生成数据
        $left_time = strtotime(date('Y-m-d', (int)$start_time));
        $right_time = strtotime(date('Y-m-d', (int)$end_time));
        var_dump('左边时间');
        var_dump($left_time);
        var_dump(date('Y-m-d  H:i:s', $left_time));
        var_dump($right_time);
        var_dump(date('Y-m-d  H:i:s', $right_time));
//        return 'hello';
        // 开启两个协程处理统计数据
        parallel([
             // 从左往右执行 每次右边递减
            function() use ($left_time,$right_time){
                while (($right_time - $left_time) > 0 ){
                    var_dump('从左往右执行,右边执行时间');
                    var_dump(date('Y-m-d  H:i:s', $right_time));
                    $right_time -= 86400;
                    // 查看统计记录是否存在
                    $has_visit = WxvisitModel::query()->where(['add_time'=>$right_time,'p_id'=>2])->exists();
                    var_dump($has_visit);
                    if(! $has_visit){
                        // 更新统计数据
                        $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$right_time,$right_time+86399])->where(['p_id'=>0])->count();
                        WxvisitModel::query()->insertGetId(['p_id' => 2, 'add_time' => $right_time, 'update_time' => $right_time+86399, 'watch' => $insert_visit]);
                    }
                }
                return '从左往右执行';
            },
            //从右往左执行
            function() use ($left_time,$right_time){
                while (($right_time - $left_time) > 0 ){
                    var_dump('从右往左执行,左边执行时间');
                    var_dump(date('Y-m-d  H:i:s', $left_time));
                    $left_time += 86400;
                    // 查看统计记录是否存在
                    $has_visit = WxvisitModel::query()->where(['add_time'=>$left_time,'p_id'=>2])->exists();
                    var_dump($has_visit);
                    if(! $has_visit){
                        // 更新统计数据
                        $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$left_time,$left_time+86399])->where(['p_id'=>0])->count();
                        WxvisitModel::query()->insertGetId(['p_id' => 2, 'add_time' => $left_time, 'update_time' => $left_time+86399, 'watch' => $insert_visit]);
                    }
                }
                return '从右往左执行';
            }
        ]);
        $weeks_data = WxvisitModel::query()->where(['p_id'=>2])
//            ->groupBy('add_time')
            ->orderBy('add_time','ASC')
            ->whereBetween('add_time',[$start_time,$end_time])
            ->select('watch','add_time')
            ->get()->toArray();
        array_push($weeks_data,['watch'=>$today_data,'add_time'=>date('Y-m-d H:i:s', strtotime(date('Y-m-d', time())))]);
        return $this->response->json(ApiUtils::send(0,'ok',['history'=>$weeks_data,'today'=>$today_data],1));
    }

    /**
     * 根据任意时间戳获取当天0点时间戳
     * Abell 2020/5/9
     * @RequestMapping(path="/abell/wxvisit/zerotime", methods="get")
     */
    public function zerotime(RequestInterface $request){
        $today_zero = strtotime(date('Y-m-d', time()));
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近19天的数据
            $start_time = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 19;
            $end_time = time();
        }
        // 搜索结束时间是当天的话就生成当天的统计
        $today_data = ($end_time > $today_zero) ? WxvisitModel::query()->where(['p_id'=>0])->where('add_time', '>', $today_zero)->count() : 0;
        // 判断搜索时间段的数据是否存在,不存在就生成数据
        $left_time = strtotime(date('Y-m-d', (int)$start_time));
        $right_time = strtotime(date('Y-m-d', (int)$end_time));
        // 开启两个协程处理统计数据
        parallel([
            // 从左往右执行 每次右边递减
            function() use ($left_time,$right_time){
                while (($right_time - $left_time) > 0 ){
                    $right_time -= 86400;
                    // 查看统计记录是否存在
                    $has_visit = WxvisitModel::query()->where(['add_time'=>$right_time,'p_id'=>2])->exists();
                    if(! $has_visit){
                        // 更新统计数据
                        $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$right_time,$right_time+86399])->where(['p_id'=>0])->count();
                        WxvisitModel::query()->insertGetId(['p_id' => 2, 'add_time' => $right_time, 'update_time' => $right_time+86399, 'watch' => $insert_visit]);
                    }
                }
                return '从左往右执行';
            },
            //从右往左执行
            function() use ($left_time,$right_time){
                while (($right_time - $left_time) > 0 ){
                    $left_time += 86400;
                    // 查看统计记录是否存在
                    $has_visit = WxvisitModel::query()->where(['add_time'=>$left_time,'p_id'=>2])->exists();
                    if(! $has_visit){
                        // 更新统计数据
                        $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$left_time,$left_time+86399])->where(['p_id'=>0])->count();
                        WxvisitModel::query()->insertGetId(['p_id' => 2, 'add_time' => $left_time, 'update_time' => $left_time+86399, 'watch' => $insert_visit]);
                    }
                }
                return '从右往左执行';
            }
        ]);
        $weeks_data = WxvisitModel::query()->where(['p_id'=>2])
            ->orderBy('add_time','ASC')
            ->whereBetween('add_time',[$start_time,$end_time])
            ->select('watch','add_time')
            ->get()->toArray();
        array_push($weeks_data,['watch'=>$today_data,'add_time'=>date('Y-m-d H:i:s', strtotime(date('Y-m-d', time())))]);
        return $this->response->json(ApiUtils::send(0,'ok',['history'=>$weeks_data,'today'=>$today_data],1));
    }

    public function delhistory($stand_time,$start_time){
        while (($stand_time - $start_time) > 0 ){
            $stand_time -= 86400;
            // 查看统计记录是否存在
            $has_visit = WxvisitModel::query()->where(['add_time'=>$stand_time,'p_id'=>2])->exists();
            var_dump($has_visit);
            if(! $has_visit){
                // 更新统计数据
                $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$stand_time,$stand_time+86399])
                    ->where(['p_id'=>0])->count();
                WxvisitModel::query()->insertGetId([
                    'p_id' => 2,
                    'add_time' => $stand_time,
                    'update_time' => $stand_time+86399,
                    'watch' => $insert_visit
                ]);
            }
        }
    }

    /**
     * 累计访问人数 旧版本同步处理
     * Abell 2020/4/26
     * @RequestMapping(path="/abell/wxvisit/visithistory_old", methods="get")
     */
    public function visithistory_old(RequestInterface $request){
        $weeks_data = [];
        $today_data = 0;
        $today_zero = strtotime(date('Y-m-d', time()));
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近19天的数据
            $start_time = $right_time = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 19;
            $end_time = time();
        }
        // 搜索结束时间是当天的话就生成当天的统计
        if($end_time > $today_zero){
            $today_data = WxvisitModel::query()->where(['p_id'=>0])->where('add_time', '>', $today_zero)->count();
        }
        // 判断搜索时间段的数据是否存在,不存在就生成数据
        $stand_time = strtotime(date('Y-m-d', time()));
        while (($stand_time - $start_time) > 0 ){
            $stand_time -= 86400;
            // 查看统计记录是否存在
            $has_visit = WxvisitModel::query()->where(['add_time'=>$stand_time,'p_id'=>2])->exists();
            if(empty($has_visit)){
                // 更新统计数据
                $insert_visit = WxvisitModel::query()->whereBetween('add_time',[$stand_time,$stand_time+86399])
                    ->where(['p_id'=>0])->count();
                $update_visit = WxvisitModel::query()->insertGetId([
                    'p_id' => 2,
                    'add_time' => $stand_time,
                    'update_time' => $stand_time+86399,
                    'watch' => $insert_visit
                ]);
            }
        }
        $weeks_data = WxvisitModel::query()->where(['p_id'=>2])
            ->orderBy('add_time','ASC')
            ->whereBetween('add_time',[$start_time,$end_time])
            ->get();
        return $this->response->json(ApiUtils::send(0,'ok',['history'=>$weeks_data,'today'=>$today_data],1));
    }

    /**
     * 累计访问人数
     * Abell 2020/4/26
     * @RequestMapping(path="/abell/wxvisit/newvisit", methods="get")
     */
    public function newvisit(RequestInterface $request){
        $weeks_data = [];
        $today_data = 0;
        $today_zero = strtotime(date('Y-m-d', time()));
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近19天的数据
            $start_time = $right_time = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 19;
            $end_time = time();
        }

        // 搜索结束时间是当天的话就生成当天的统计
        if($end_time > $today_zero){
            $today_data = WxvisitModel::query()->where(['p_id'=>0])->where('add_time', '>', $today_zero)->count();
        }
    }

    /**
     * 获取基本的访问数据
     * Abell 2020/4/26
     * @RequestMapping(path="/abell/wxvisit/visitbase", methods="get")
     */
    public function visitbase(RequestInterface $request){
        $today_zero = strtotime(date('Y-m-d', time()));
        $count_all = WxvisitModel::query()->where(['type_explain' => 'login2reg', 'p_id' => 0])->count();
        $count_today = WxvisitModel::query()
            ->where(['type_explain' => 'login2reg', 'p_id' => 0])
            ->where('add_time', '>', $today_zero)
            ->count();
        //总添加人数  新添加人数(当天添加人数)
        $add_all = WxuserModel::query()->where(['hasadd' => 1])->count();
        $add_today = WxuserModel::query()->where(['hasadd' => 1])
            ->where('add_time', '>', $today_zero)->count();
        return $this->response->json(ApiUtils::send(0,'ok',['all' => $count_all, 'today' => $count_today,
            'add_all'=>$add_all,'add_today'=>$add_today],1));
    }

    /**
     * 生成按天统计子页面的访问数据
     * Abell 2020/4/26
     *
     * @RequestMapping(path="/abell/wxvisit/webs", methods="get")
     */
    public function webs(RequestInterface $request){
        if($request->has('start_time')) {
            $start_time = $request->input('start_time');
            if ($request->has('end_time')) {
                $end_time = $request->input('end_time');
            } else {$end_time = time();}
        }else{
            // 默认显示最近1周的数据
            $start_time = strtotime(date('Y-m-d')) - (60 * 60 * 24) * 7;
            $end_time = time();
        }
        $type_explains = WxvisitModel::query()
            ->select('type_explain')
            ->whereBetween('p_id',[0,1])
            ->distinct()
            ->get();
        $type_explains_array = json_decode(json_encode($type_explains),TRUE);
        $type_explains_arr = array_column($type_explains_array,'type_explain');
        // 循环搜索每个子页面的访问数据
        $visit_data = [];
        $visit_num_all = WxvisitModel::query()
            ->where(['p_id'=>0])
            ->whereBetween('add_time',[$start_time,$end_time])
            ->count();

        foreach ($type_explains_arr as $type_str){
            $visit_num = WxvisitModel::query()
                ->where(['type_explain'=>$type_str])
                ->whereBetween('add_time',[$start_time,$end_time])
                ->count();
            array_push($visit_data,[
                'type_explain'=>$type_str,
                'watch'       => $visit_num,
                'proportion'  => empty($visit_num_all) ? 0 : round($visit_num/$visit_num_all*100)
            ]);
        }
        return $this->response->json(ApiUtils::send(0,'ok',['data'=>$visit_data,
            'start_time' => date( "Y-m-d",(int)$start_time),
            'end_time'    => date( "Y-m-d",(int)$end_time)],1));
    }

    /**
     * 获取小程序特定时间段内的详细访问数据
     * Abell 2020/4/23
     *
     * @RequestMapping(path="/abell/wxvisit/days", methods="get")
     */
    public function days(RequestInterface $request){
        $map = [];
        if($request->has('start_time')){//如果没有开始时间默认显示当天的数据
            $map['start_time'] = $request->input('start_time');
        }else{
            $map['start_time'] = strtotime(date('Y-m-d', time()));
        }
        if($request->has('end_time')){
            $map['end_time'] = $request->input('end_time');
        }else{
            $map['end_time'] = time();
        }
        // 普通按天循环显示数据 显示每天的队列生成的汇总数据 默认显示最近7天的数据
        $weeks_data =  WxvisitModel::query()->whereBetween('add_time', [$map['start_time'], $map['end_time']])->get();
        return $this->response->json(ApiUtils::send(0,'ok',$weeks_data,1));
    }

    /**
     * 获取小程序特定时间段内的子页面详细访问数据
     * Abell 2020/4/23
     * @RequestMapping(path="/abell/wxvisit/subwebs", methods="get")
     */
    public function subwebs(RequestInterface $request){
        $map = [];
        if($request->has('start_time')){//如果没有开始时间默认显示当天的数据
            $map['start_time'] = $request->input('start_time');
        }else{
            $map['start_time'] = strtotime(date('Y-m-d', time()));
        }
        if($request->has('end_time')){
            $map['end_time'] = $request->input('end_time');
        }else{
            $map['end_time'] = time();
        }
        if($request->has('type_explain')){ //如果不传子目录,默认显示登录注册的记录
            $map['type_explain'] = $request->input('type_explain');
        }else{
            $map['type_explain'] = 'login2reg';
        }
        $web_data = WxvisitModel::query()
            ->whereBetween('add_time', [$map['start_time'], $map['end_time']])
            ->where(['type_explain'=>$map['type_explain']])
            ->get();
        return $this->response->json(ApiUtils::send(0,'ok',$web_data,1));
    }



}